package com.hzhr.iot.user.controller;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.Author;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.user.controller.base.BaseController;
import com.hzhr.iot.user.model.user_sysInfo.User_SysInfoGetModel;
import com.hzhr.iot.user.model.user_sysInfo.User_SysInfoPutModel;
import com.hzhr.iot.user.service.User_SysInfoService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

@Api(tags = "User_SysInfo", description = "用户的系统信息")
@RestController
@RequestMapping(value = "")
public class User_SysInfoController extends BaseController {
    @Autowired
    User_SysInfoService user_sysInfoService;

    //region /users/{userId}/sys-infos PUT
    //region swagger响应码 : putByUserId
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
    })
    //endregion
    //region swagger说明 : putByUserId
    @ApiOperation(value="管理员更新管辖范围下的用户的系统信息", notes="管理员更新管辖范围下的用户的系统信息  \n简介 : 更新用户的系统信息  \n步骤 :  \n1. 验证角色的合法性 :  \n 根据登录用户的companyId查询公司(COMPANY)company_1,  \n 如果company_1.type为燃气公司，body.firstRoleId只能为1或2，否则抛出异常201；  \n 如果company_1.type为物联网设备公司，body.firstRoleId只能为3或4，否则抛出异常201；  \n2. 查询用户 :  \n 根据路径参数userId查询登录用户管辖范围下的用户(USER)user_1，筛选条件：user_1.id = userId  \n 如果user_1不存在，抛出异常202；  \n3. 验证新的部门编号的合法性 :  \n 如果body.deptId != user_1.deptId，则根据body.deptId查询登录用户管辖范围内的部门(DEPT)dept_1，如果dept_1不存在，抛出异常203；  \n4. 更新用户信息 :  \n 根据body更新user_1的相关属性，并同时设置以下属性：  \n (1) user_1.userCode = dept_1.resourceCode；  \n (2) user_1.resourceCode = dept_1.resourceCode；  \n5. 更新用户角色 :  \n 根据user_1.id查询用户角色关系(USER_IN_ROLE)，筛选出roleId值最小的那个用户角色关系(USER_IN_ROLE)userInRole_1，  \n 如果userInRole_1存在，则设置属性：userInRole_1.roleId = body.firstRoleId  \n6. 返回信息 :  \n 根据条件参数查询更新结果，组装响应并返回；  \n",produces = "application/json")
    //endregion
    //region swagger参数 : putByUserId
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "userId", value = "用户编号",required = true, dataType = "int", paramType = "path"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })


    //endregion
    //region 请求 : putByUserId
    @RequestMapping(value = "/users/{userId}/sys-infos", method = RequestMethod.PUT)
    //endregion
    @Author
    public ResponseEntity<User_SysInfoGetModel> putByUserId(@PathVariable("userId") Integer userId,
                                                            @RequestParam @ApiIgnore Map<String, Object> query,
                                                            @RequestBody User_SysInfoPutModel body) throws ServiceException {
        User_SysInfoGetModel responseBody = user_sysInfoService.putByUserId(initHeader(), userId, query, body);
        return new ResponseEntity<>(responseBody, HttpStatus.OK);
    }
    //endregion
    //region /users/sys-infos GET
    //region swagger响应码 : get
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功"),
    })
    //endregion
    //region swagger说明 : get
    @ApiOperation(value="管理员查询其管辖范围下的用户的系统信息", notes="管理员查询其管辖范围下的用户的系统信息  \n简介 : 查询用户的系统信息  \n步骤 :  \n1. 查询用户 : (√)  \n 根据条件参数查询用户(USER)；  \n2. 返回信息 : (√)  \n 根据条件参数和查询到的结果组装响应并返回；  \n",produces = "application/json")
    //endregion
    //region swagger参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where user_sysInfo.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where user_sysInfo.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),

    })

    //endregion
    //region 请求 : get
    @RequestMapping(value = "/users/sys-infos", method = RequestMethod.GET)
    //endregion
    @Author
    public ResponseEntity<User_SysInfoGetModel> get(@RequestParam @ApiIgnore Map<String, Object> query) throws ServiceException {
        User_SysInfoGetModel responseBody = user_sysInfoService.get(initHeader(), query);
        return new ResponseEntity<>(responseBody, HttpStatus.OK);
    }
    //endregion
}
















































































































































































































































































































































































